"use strict";
/**
 * 登录
 */  
/*var loginedToDashbord = function(){		
       var newCookie = new sweetCookie();
       var hasUserName = newCookie.getCookie("userName");
       if(hasUserName){  //已登录，返回登录欢迎界面;
    	   window.location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/basicInfo/index.html?selectedNav=0&navPos=0";
       }
};*/
$(function(){ 
    
	//loginedToDashbord();
	var rememberMe = false;
	var tempUsername;
	var tempPassword;

	//获取保存的密码 在页面显示
	$.post("/Lvscore_Advertiser_Maven/getUser", "",
		function(data){
			//alert("从服务器获取密码！");
		   if(data.donkeynum==1){
			   window.location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/basicInfo/index.html?selectedNav=0&navPos=0";
		   }
		
			if (data != null) {
				$("#flag").val("1");
				$("#username").val(data.username);
				$("#password").val(data.password);
				tempUsername = data.username;
				tempPassword = data.password;
				if(data.password !=null){
					$(".rememberMe img").attr("class","show");
					rememberMe = true;
				}else{
					$(".rememberMe img").attr("class","hide");
				}
			}
		});

/*	$("#password").change(function() {
		$("#flag").val("");
	});*/
	$("#username").on("keyup",function(){
		$("#flag").val("");
	});
	$("#password").on("keyup",function(){
		$("#flag").val("");
	});
	

	//用户名框获取焦点时，清空密码框
	$("#username").focus(function (){ 
		$("#password").val(""); 
	});
	$("#username").blur(function() {
		if($("#username").val() == tempUsername){
			$("#password").val(tempPassword);
		}
	});

	function loginIn(){
	     //过滤无效请求;
		 var username = $("#username").val(),password = $("#password").val(),loginErrorMsgObj = $("#loginErrorMsg");
		var flag = $("#flag").val();
		console.log(flag);
		 if(!username||!password){
		     loginErrorMsgObj.text("账号或密码不能为空！").show();
		 }else{
		     loginErrorMsgObj.hide();
		     $.ajax({
			     url:"/Lvscore_Advertiser_Maven/dologin",
				 data:{memberName:username,password:password,rememberMe:rememberMe,flag:flag},
				 dataType:"json",
				 type:"post",
				 success:function(json){ 
					 $("#username").val("");
					 $("#password").val("");
					 if(json.status=="1"){  //验证成功!
//						 var newCookie = new sweetCookie();
//						 newCookie.setCookie("userName",json.data.userName,30*60*1000);
						 //$("#loginA").attr("href","/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/basicInfo/index.html?selectedNav=0&navPos=0");
						 
						 window.location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/basicInfo/index.html?selectedNav=0&navPos=0";
						 //$("#loginA span").click();
					 }else{ //验证未通过
	                     loginErrorMsgObj.text(json.error).show();
	                     $("#username").val(username);
					 }
				 },			 
			 });
		 }
	}

	$(".loginSubmitBtn").on("click",function(){
		loginIn();
	});
     //记住密码图标切换;rememberMe
	$(".rememberMe").click(function(){
		var _this = $(this).find(".checkBoxCircle");
		var chooseStatus=$(".rememberMe img").attr("class");
		if(chooseStatus=="hide"){
			_this.find("img").toggleClass("hide show");
			rememberMe = true;
		}else{
			_this.find("img").toggleClass("show hide");
			rememberMe = false;
		}
	});
 	$("body").keydown(function(event) {
 	    if (event.keyCode == "13") {//keyCode=13是回车键
 	        $('#loginA').click();
 	    }
 	});  
});